<?php
/**
 * @Copyright   2008, XXXX-XXXX
 * All rights reserved. 
 * feature summary.
 * @filesource  config.php 
 * @author      lipeng <lipengage@gmain.com> 
 * @version     $Id: $ 
 * @package     application
 * @subpackage  / 
 */

/*
 * the class read a config file, return the config file member
 *     and application config file, return the config file member
 */
class config{
	static private $config = false;
	static private $app_config = false;

	/**
	 * read the base config file
	 */
	private static function load_config() {
		if (!self::$config) {
			require_once 'config/application.php';
			self::$config = $data_conf;
		}
	}

	/**
	 * read a app config file
	 */
	private static function load_app_config() {
		if (!self::$app_config) {
			$uri = $_SERVER['REQUEST_URI'];
			global $servletMap;
			$conf_dir = false;
			foreach ($servletMap as $url => $class) {
				$len = strlen($url);
				if (substr($uri, 0, $len) == $url) {
					$conf_dir = substr($uri, $len+1, strpos($uri,'/',$len+1)-$len-1);
					break;
				}
			}
			$conf = self::get($conf_dir.'_conf');
			if (is_file($conf)) {
				require_once $conf;
				self::$app_config = $conf;
			}
			else {
				header('HTTP/1.1 500 Internal Server Error');
				echo '<html><body><h1>URI ERROR</h1></body></html>';
				exit;
			}
		}
	}

	/**
	 *get value from file "config/application.php"
	 *
	 *@param $key
	 */
	static function get($key) {
		if (!self::$config) {
			self::load_config();
		}
		if (isset(self::$config[$key])) {
			return self::$config[$key];
		}
	}

	/**
	 *get value from file "app..."
	 *
	 *@param $key
	 */
	static function app_get($key) {
		if (!self::$app_config) {
			self::load_app_config();
		}
		if (isset(self::$app_config[$key])) {
			return self::$app_config[$key];
		}
	}
}